//
//  DataBaseManager.m
//  UI23_FMDB
//
//  Created by dllo on 16/3/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DataBaseManager.h"
#import "CaseBaseModel.h"

@implementation DataBaseManager

+(DataBaseManager *)shareInstance
{
    static DataBaseManager *dbManager = nil;
    if (!dbManager) {
        dbManager = [[DataBaseManager alloc] init];
    }
    return dbManager;
}
//打开数据库
-(void)openDB
{
    //Documents 路径
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSLog(@"%@",path);
    //自定义子文件夹
    NSString *filePath = [path stringByAppendingPathComponent:@"my"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    //拼接数据库路径(.db/.sqlite)
    self.filePath =[NSString stringWithFormat:@"%@/my/collect.db",path];;
    //创建数据库对象
    self.db = [[FMDatabase alloc] initWithPath:_filePath];
    //打开数据库
    BOOL result = [_db open];
    if (result) {
        NSLog(@"打开数据库成功");
    }else{
        NSLog(@"打开失败");
    }
}
//关闭数据库
-(void)closeDB
{
    [_db close];
}
//创建表
-(void)createTable
{
    //创建表(SQL语句)
    //主键 primary key(唯一标识 ->每一条数据)
    //自增 autoincrement
    NSString *createSQL = @"CREATE TABLE IF NOT EXISTS cased(title TEXT PRIMARY KEY, coverPhoto TEXT)";
    [self executeSQL:createSQL message:@"创建收藏表"];
}
-(void)createTableS
{
    //创建表(SQL语句)
    //主键 primary key(唯一标识 ->每一条数据)
    //自增 autoincrement
    NSString *createSQL = @"CREATE TABLE IF NOT EXISTS photos(mId TEXT PRIMARY KEY, width TEXT, height TEXT)";
    [self executeSQL:createSQL message:@"创建图片表"];
}

//关闭表
-(void)dropTable
{
    NSString *dropSQL = @"DROP TABLE cased";
    [self executeSQL:dropSQL message:@"删除表"];
}
-(void)dropTablePhoto
{
    NSString *dropSQL = @"DROP TABLE photos";
    [self executeSQL:dropSQL message:@"删除表"];
}
//增加数据
-(void)insertData:(NSString *)title photo:(NSString *)coverPhoto
{
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO cased(title, coverPhoto) VALUES('%@','%@')",title,coverPhoto];
    [self executeSQL:insertSQL message:@"收藏案例"];
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
   // NSLog(@"%@",path);
    //拼接文件路径
    NSString *pathStr = [path stringByAppendingPathComponent:@"str.txt"];
    //写本地
    //参数1.文件路径
    //参数2.线程安全
    //参数3.编码方式
    //参数4.错误信息
    [title writeToFile:pathStr atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //从本地读取数据
   // NSString *newStr = [NSString stringWithContentsOfFile:pathStr encoding:NSUTF8StringEncoding error:nil];
}
-(void)insertGalBaseModel:(GalBaseModel *)m
{
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO photos(mId, width, height) VALUES('%@','%@','%@')",m.mId,m.width,m.height];
    [self executeSQL:insertSQL message:@"收藏图片"];
}
//删除
-(void)deleteBytitle:(NSString *)title
{
    NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM cased WHERE title = '%@'",title];
    [self executeSQL:deleteSQL message:@"删除数据"];
}
-(void)deleteBymId:(NSNumber *)mId
{
    NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM photos WHERE mId = '%@'",mId];
    [self executeSQL:deleteSQL message:@"删除数据"];
}

//查询
-(BOOL)selectCaseBytitle:(NSString *)title
{
    NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM cased WHERE title = '%@'", title];
    FMResultSet *set = [_db executeQuery:selectSQL];
    return [set next];
}
-(BOOL)selectCaseBymId:(NSString *)mId
{
    NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM photos WHERE mId = '%@'", mId];
    FMResultSet *set = [_db executeQuery:selectSQL];
    return [set next];
}

//查询所有
-(NSMutableArray *)selectAll
{
    //获取所在表所有数据
    //获取部分 +WHERE.....
    NSString *selectSQL = @"SELECT * FROM cased";
    //    查询
    FMResultSet *set = [_db executeQuery:selectSQL];
    NSMutableArray *arr = [NSMutableArray array];
    //    遍历获取每个查询数据
    while ([set next]) {
        //        获取每一列信息
        CaseBaseModel *m = [[CaseBaseModel alloc] init];
        m.title = [set stringForColumn:@"title"];
        m.coverPhoto = [set stringForColumn:@"coverPhoto"];
        [arr addObject:m];
    }
    return arr;
}
-(NSMutableArray *)selectAllphotos
{
    //获取所在表所有数据
    //获取部分 +WHERE.....
    NSString *selectSQL = @"SELECT * FROM photos";
    //    查询
    FMResultSet *set = [_db executeQuery:selectSQL];
    NSMutableArray *arr = [NSMutableArray array];
    //    遍历获取每个查询数据
    while ([set next]) {
        //        获取每一列信息
        GalBaseModel *m = [[GalBaseModel alloc] init];
        m.mId = [set stringForColumn:@"mId"];
        m.width = [set stringForColumn:@"width"];
        m.height = [set stringForColumn:@"height"];
        [arr addObject:m];
    }
    return arr;
}

//-(void)insertMoreData
//{
//    //使用FMDataBaseQueue队列 完成多线程操作
//    FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:_filePath];
//    //transaction 数据处理
//    [queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
//       //要执行的操作
//         NSString *insertSQL = @"INSERT INTO Case(name,sex,score) VALUES('欧阳','男',18)";
//        BOOL r = [db executeUpdate:insertSQL];
//        if (r) {
//            NSLog(@"批量操作成功");
//        }
//    }];
//    
//}

//封装 执行更新
-(void)executeSQL:(NSString *)sql message:(NSString *)mes
{
    BOOL result = [_db executeUpdate:sql];
    if (result) {
        NSLog(@"%@成功",mes);
    }else{
        NSLog(@"%@失败",mes);
    }
}
























@end
